import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex); 
import * as api from "../api";
import aggregate from '@/router/aggregate';
import showshowway from '@/router/showshowway';
import cancanneed from '@/utils/cancanneed'; 
import router from "../router";
let userInfo = localStorage.getItem("lex-userInfo") || "{}"
let currentChargeProduct = localStorage.getItem("lex-currentChargeProduct") || "[]"
export default new Vuex.Store({
  state: {
    userInfo: JSON.parse(userInfo),
    sideMenu: [],
    breadCrumb: [],
    dialogVisible: false,
    dialogTitle: "增加学员信息",
    stuData: [],
    loading: true,
    currentRow: {},
    stuTotal: 0,
    stuPageSize: 10,
    orderData: [],
    currentChargeProduct: JSON.parse(currentChargeProduct),
    order_id:""
  }, 
  mutations: {
    SHOW_LOADING(state) {
      state.loading = true;
    },
    HIDE_LOADING(state) {
      state.loading = false;
    },
    SET_STU_DATA(state, payload) {
      state.stuData = payload.data;
      state.loading = false;
      state.stuTotal = payload.total;
    },
    SHOW_DIALOG(state) {
      state.dialogVisible = true;
    },
    HIDE_DIALOG(state) {
      state.dialogVisible = false
    },
    CHANGE_DIALOG_TITLE(state, payload) {
      state.dialogTitle = payload;
    },
    SET_USER_INFO(state, payload) {
      state.userInfo = payload;
    },
    SET_MENULIST(state, payload) {
      let target = showshowway.find(item => item.path === "/");
      target.children = [...payload, ...target.children];
      router.addRoutes(showshowway)
      let excludeRoutes = ["pre_order", "payment"]

      target.children = target.children.filter(item => !excludeRoutes.includes(item.path))

      state.sideMenu = target.children;

    },
    SET_BREADCRUMB(state, payload) {
      state.breadCrumb = payload;
    },
    SET_CURRENT_ROW(state, payload) {
      state.currentRow = { ...payload }
    },
    SET_ORDER_DATA(state, payload) {
      state.orderData = payload
    },
    SET_CURRENT_CHARGE_PRODUCT(state, payload) {
      state.currentChargeProduct = [payload];
      localStorage.setItem('lex-currentChargeProduct', JSON.stringify([payload]))
    },
    SET_ORDER_ID(state,payload){
      state.order_id = payload;
    }
  },
  getters: {

  },
  actions: {
    async GET_MENULIST({ commit }) {
      console.log('action执行了')
      let res = await api.getMenulistApi();
      let { menuList } = res.data;
      let userRoutes = cancanneed(menuList, aggregate)
      commit("SET_MENULIST", userRoutes)
    },
    GET_STULIST({ commit }, payload = {}) {
      commit("SHOW_LOADING")
      let { page = 1, count = 10, classes = "" } = payload;
      api.getStuListApi(page, count, classes)
        .then(res => {
          commit("SET_STU_DATA", res.data)
        })
    },
    GET_ORDER_DATA({ commit }, payload) {
      let order_id = payload || "";
      api.getOrderApi(order_id)
        .then(res => {
          console.log(res.data)
          if (res.data.state) {
            console.log(res.data.data)
            commit("SET_ORDER_DATA", res.data.data)
          } else {
            this.$message.error("获取订单失败")
          }
        })
    }
  },
  modules: {},
});